package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sggg.entity.SgDrugRecord;
import org.jeecg.modules.sggg.service.ISgDrugRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 药品库
 * @Author: jeecg-boot
 * @Date: 2022-10-26
 * @Version: V1.0
 */
@Api(tags = "药品库")
@RestController
@RequestMapping("/sggg/sgDrugRecord")
@Slf4j
public class SgDrugRecordController extends JeecgController<SgDrugRecord, ISgDrugRecordService> {

    @Autowired
    private ISgDrugRecordService sgDrugRecordService;

    @ApiOperation(value = "药品库-分页列表查询", notes = "药品库-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgDrugRecord>> queryPageList(SgDrugRecord sgDrugRecord,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<SgDrugRecord> queryWrapper = QueryGenerator.initQueryWrapper(sgDrugRecord, req.getParameterMap());
        Page<SgDrugRecord> page = new Page<>(pageNo, pageSize);
        IPage<SgDrugRecord> pageList = sgDrugRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "药品库-添加")
    @ApiOperation(value = "药品库-添加", notes = "药品库-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgDrugRecord sgDrugRecord) {
        sgDrugRecordService.save(sgDrugRecord);
        sgDrugRecordService.riderSync();
        return Result.OK("添加成功！");
    }


    @AutoLog(value = "药品库-编辑")
    @ApiOperation(value = "药品库-编辑", notes = "药品库-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgDrugRecord sgDrugRecord) {
        sgDrugRecordService.updateById(sgDrugRecord);
        sgDrugRecordService.riderSync();
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "药品库-通过id删除")
    @ApiOperation(value = "药品库-通过id删除", notes = "药品库-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgDrugRecordService.removeById(id);
        sgDrugRecordService.riderSync();
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "药品库-批量删除")
    @ApiOperation(value = "药品库-批量删除", notes = "药品库-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgDrugRecordService.removeByIds(Arrays.asList(ids.split(",")));
        sgDrugRecordService.riderSync();
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "药品库-查询redis", notes = "药品库-查询redis")
    @GetMapping(value = "/queryRedis")
    public Result<Object> queryRedis() {
        return Result.OK(sgDrugRecordService.queryRedis());
    }

    @ApiOperation(value = "药品库-通过id查询", notes = "药品库-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgDrugRecord> queryById(@RequestParam(name = "id") String id) {
        SgDrugRecord sgDrugRecord = sgDrugRecordService.getById(id);
        if (sgDrugRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgDrugRecord);
    }

    /**
     * 查询药品
     *
     * @return
     */
    @GetMapping("/getDrugRecord")
    public Result<List<SgDrugRecord>> getDrugRecord() {
        return Result.OK(sgDrugRecordService.list(null));
    }


    /**
     * 根据机构查询药品
     *
     * @return
     */
    @GetMapping("/queryByorgCode")
    public Result<List<SgDrugRecord>> queryByorgCode() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        return Result.OK(sgDrugRecordService.list(new QueryWrapper<SgDrugRecord>().like("sys_org_code",sysUser.getOrgCode())));
        return Result.OK(sgDrugRecordService.list());

    }
}
